/**
 * There are <a href="https://www.hdzhx.com">邯郸智讯</a> code generation
 */
package com.boxin.ims.modules.ad.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import com.boxin.ims.modules.wechat.entity.BusinessConfig;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 广告Entity
 * @author Jakemanse
 * @version 2014-04-18
 */
@Entity
@Table(name = "ad_advertisement")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Advertisement extends BaseEntity {
	
	private static final long serialVersionUID = 1L;
	private Long id; 		// 编号
	private User user; 		// 所属人
	private User advertiser; 		// 广告主
	private String name; 	// 奖品名称
	private String type;	//投放广告方式类型    0:签到     1:抽奖            3:商家抽奖    4:会员中心     5:活动中心
	private Integer repeatCount;	//允许重复数	每位会员允许的次数
	private String repeatCycleCount;//  1为当周  			2当月
	
	
	private String content;		//广告推广内容
	private String title;		//图文标题
	private String description;		//图文描述
	private String picName;				//图片
	private String url;			//图文链接
	
	
	private Integer score;	//是否增加积分
	private Integer money;	//是否送奖金
	private Integer validDays;	//有效天数
	private String validCycleEndday;//  1为当周  			2当月
	
	private Boolean startup;	//是否启动该活动
	
	private Date createTime;	// 创建日期
	private Date updateTime;	// 更新时间
	private Date endTime;		//过期时间
	
	@Transient
	private Long tempId;			//临时变量
	
	
	private List<User> sellerList  =  Lists.newArrayList();		//广告商

	@Transient
	private String sellerIds;
	@Transient
	private String sellerNames;

	public Advertisement() {
		this.createTime = new Date();
	}

	public Advertisement(Long id){
		this();
		this.id = id;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	//@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_ad_advertisement")
	//@SequenceGenerator(name = "seq_ad_advertisement", sequenceName = "seq_ad_advertisement")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name="user_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@Length(min=1, max=200)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
	@NotNull
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Integer getRepeatCount() {
		return repeatCount;
	}

	public void setRepeatCount(Integer repeatCount) {
		this.repeatCount = repeatCount;
	}

	public Integer getScore() {
		return score;
	}

	public void setScore(Integer score) {
		this.score = score;
	}

	public Integer getMoney() {
		return money;
	}

	public void setMoney(Integer money) {
		this.money = money;
	}

	public Integer getValidDays() {
		return validDays;
	}

	public void setValidDays(Integer validDays) {
		this.validDays = validDays;
	}


	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	@ManyToOne
	@JoinColumn(name="advertiser_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotNull
	public User getAdvertiser() {
		return advertiser;
	}

	public void setAdvertiser(User advertiser) {
		this.advertiser = advertiser;
	}

	
	
	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "ad_advert_to_user", joinColumns = { @JoinColumn(name = "ad_id") }, inverseJoinColumns = { @JoinColumn(name = "user_id") })
	@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
	@OrderBy("id") @Fetch(FetchMode.SUBSELECT)
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@NotEmpty
	public List<User> getSellerList() {
		return sellerList;
	}

	public void setSellerList(List<User> sellerList) {
		this.sellerList = sellerList;
	}

	
	@Transient
	public List<Long> getSellerIdList() {
		List<Long> sellerIdsList = Lists.newArrayList();
		for (User user : sellerList) {
			sellerIdsList.add(user.getId());
		}
		return sellerIdsList;
	}
	
	
	
	@Transient
	public void setSellerIdList(List<Long> sellerIdList) {
		sellerList = Lists.newArrayList();
		for (Long configId : sellerIdList) {
			User  user = new User(configId);
			sellerList.add(user);
		}
	}

	@Transient
	public String getSellerIds() {
		sellerIds = "";
		for (User user : sellerList) {
			Long id = user.getId();
			sellerIds +=""+id+",";
		}
		if(StringUtils.isNotEmpty(sellerIds)){
			sellerIds  = sellerIds.replaceAll(",$", "");
		}
		return sellerIds;
	}

	
	@Transient
	public void setSellerIds(String sellerIds) {
		this.sellerIds = sellerIds;
		
		sellerList = Lists.newArrayList();
		if(StringUtils.isNotEmpty(sellerIds)){
			String ids[] = sellerIds.split(",");
			if(ids!=null && ids.length>0){
				for(String sid : ids){
					Long id = Long.parseLong(sid);
					User  user = new User(id);
					sellerList.add(user);
				}
			}
		}
	}
	
	
	
	@Transient
	public String getSellerNames() {
		sellerNames = "";
		if(sellerList!=null && !sellerList.isEmpty()){
			for(User u : sellerList){
				sellerNames += u.getName()+",";
			}
			
			if(StringUtils.isNotEmpty(sellerNames)){
				sellerNames  = sellerNames.replaceAll(",$", "");
			}
			
		}
		return sellerNames;
	}

	
	@Transient
	public void setSellerNames(String sellerNames) {
		this.sellerNames = sellerNames;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}


	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}


	public String getPicName() {
		return picName;
	}

	public void setPicName(String picName) {
		this.picName = picName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Transient
	public Long getTempId() {
		return tempId;
	}

	
	public void setTempId(Long tempId) {
		this.tempId = tempId;
	}
	
	

	public String getRepeatCycleCount() {
		return repeatCycleCount;
	}

	public void setRepeatCycleCount(String repeatCycleCount) {
		this.repeatCycleCount = repeatCycleCount;
	}

	public String getValidCycleEndday() {
		return validCycleEndday;
	}

	public void setValidCycleEndday(String validCycleEndday) {
		this.validCycleEndday = validCycleEndday;
	}

	public Boolean getStartup() {
		return startup==null ? true:startup;
	}

	public void setStartup(Boolean startup) {
		this.startup = startup;
	}
	
	@Transient
	public boolean isStartup(){
		return this.startup != null && startup;
	}
	
	
	

	
}


